<?php
require_once(SCRIPTS_PATH . '/phpthumb/ThumbLib.inc.php');

class Zendvn_File_Image extends PhpThumbFactory{
	
	/*=========================================================
	 * Resize Img theo 2 dang Relative va Absolute
	 * Relative: bop anh lai dung kich thuoc dua ra, anh co the bi meo xau
	 * Absolute: Resize anh theo kich thuoc dua ra, tinh toan va cat anh cho phu hop
	 * Cac tham so truyen vao dang array
	 * $fileName: Ten cua tap tin duoc upload(co the da resize)
	 * $arrParam: Mang chua kich thuoc(Height, width) can resize, duong dan den thu muc se upload
	 * $options: Mang chua kieu resize ('relative', 'absolute')
	 *=========================================================*/
	public static function resizeImg($fileName, $arrParam = null, $options = null){
		/*=================================================
		 * Resize upload nhieu thu muc
		 * Vd: resize vao 2 thu muc thumb, thumb1
		 * Cac param la 1 mang chua cac thong tin ve thu muc upload, size.
		 *=================================================*/
		if(!empty($options['type'])){
			foreach ($options['type'] as $k => $v){
				$size = array('width' => $arrParam['thumb-width'][$k], 'height' => $arrParam['thumb-height'][$k]);
				$dir  = array('uploadDir' => $arrParam['uploadDir']. '/' . $fileName,
							  'thumbDir' => $arrParam['thumbDir'][$k]. '/' . $fileName
							);
				if(empty($options['crop'])){
					self::proccess($v, $size, $dir);
				}else{
					self::proccess($v, $size, $dir, $options['crop']);
				}
			}
		}
	}
	
	/*=====================================================
	 * Xử lý resize
	 * $type = absolute | relative
	 * $size = array('height', 'width')
	 * $dir  = array('thumbDir', 'uploadDir')
	 *=====================================================*/
	public static function proccess($type, $size, $dir, $options = null){
		$img = self::create($dir['uploadDir']);
		//=============== Resize tuong doi voi type = relative ======================
		if($type == 'relative'){
			$img->resize($size['width'], $size['height'])
				->save($dir['thumbDir']);
		}
		//=============== Resize tuyet doi voi type = absolute =====================
		if($type == 'absolute'){
			$sizeImge = @getimagesize($dir['uploadDir']);
			//============ Lay width va height cua anh goc dc upload ===========
			$width 	= $sizeImge[0];
			$height	= $sizeImge[1];
			//============ Tinh toan height width de resize ve huong ngan hon ===========
			if($width >= $height){
				$ratio = round($size['height'] / $height);
	      		$width = $width * $ratio;
				$img->resize($width, $size['height']);
			}else{
				$ratio = round($size['width'] / $width);
	      		$height = $height * $ratio;
				$img->resize($size['width'], $height);
			}
			//============ Cat anh sau khi da resize ve 1 huong ===============
			if(empty($options)){
				$img->save($dir['thumbDir'])
					->cropFromCenter($size['width'],$size['height'])
					->save($dir['thumbDir']);
			}else{
				$img->save($dir['thumbDir'])
					->crop(0, 0, $size['width'],$size['height'])
					->save($dir['thumbDir']);
			}	
		}
	}
	
}







